﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XML_Indexing;
using CDE_Model;

namespace XQuery
{
    /// <summary>
    /// XQueryEvaluator object that can evaluate queries against an index
    /// </summary>
    public class XQueryEvaluator
    {
        private String f_query;
        private IXMLIndex f_index;

        /// <summary>
        /// Gets or sets the index for the XQueryEvaluator object.
        /// The index that is passed here is the index that
        /// will be used to evaluate the document
        /// </summary>
        /// <remarks>
        /// Using an AnvilIndex is similar to using no index
        /// at all. Avoid using this index if at all possible!
        /// </remarks>
        public IXMLIndex Index
        {
            get { return f_index; }
            set { f_index = value; }
        }

        /// <summary>
        /// Gets or sets the query string for the XQueryEvaluator object.
        /// </summary>
        /// <remarks>
        /// Currently only supports simple XPath expressions
        /// </remarks>
        public String Query
        {
            get { return f_query; }
            set { f_query = value; }
        }

        /// <summary>
        /// Constructs an XQueryEvaluator object
        /// </summary>
        /// <param name="query">The query to pass into the object that will be evaluated</param>
        public XQueryEvaluator(IXMLIndex index, String query)
        {
            Query = query;
            Index = index;
        }

        /// <summary>
        /// Evaluates the given query and returns the element tree
        /// </summary>
        public List<Element> Evaluate()
        {
            return XQueryParser.Parse(Index, Query);
        }
    }
}
